﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dominio;
using db;

namespace FormsUI
{
    public partial class FormPrincipal : Form
    {
        private TreeView treeViewDepositos;
        public FormPrincipal()
        {
            InitializeComponent();
            cargarDepositos();
            configurarGrilla();
        }

        private void cargarDepositos()
        {
            cargarArbol();
            CargarDatos();
         
        }

        private void configurarGrilla()
        {
            grilla.Dock = DockStyle.Fill;
            grilla.BorderStyle = BorderStyle.Fixed3D;
           //grilla.AutoGenerateColumns = false;

        }

        private void cargarArbol()
        {
            treeViewDepositos = new TreeView();
            splitContainer.Panel1.Controls.Add(treeViewDepositos);
            treeViewDepositos.BorderStyle = BorderStyle.Fixed3D;
            treeViewDepositos.Dock = DockStyle.Fill;
            
            
            //treeViewPlanta.AfterSelect += new TreeViewEventHandler(treeViewPlanta_AfterSelect);
            //treeViewPlanta.AfterLabelEdit += new NodeLabelEditEventHandler(treeViewPlanta_AfterLabelEdit);
            treeViewDepositos.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeViewDepositos_NodeMouseClick);
            ImageList iconos = new ImageList();
            iconos.Images.Add(global::FormsUI.Properties.Resources.Folder_icon);
            treeViewDepositos.ImageList = iconos;
        }
        private void CargarDatos()
        {

            Sistema sis = Sistema.getInstance();
            Deposito actual = sis.GetDepositoActual();
            TreeNode[] sectores = new TreeNode[actual.Sector.Count()];
            int i = 0;
            foreach (Sector s in actual.Sector)
            {
                sectores[i] = new TreeNode(s.Nombre);
                sectores[i].Tag = s;
                sectores[i].ImageIndex = 0;
                i++;
            }
            TreeNode raiz = new TreeNode(actual.Nombre,sectores);
            raiz.ImageIndex = 0;
            treeViewDepositos.Nodes.Add(raiz);
            
            foreach (TreeNode o in treeViewDepositos.Nodes)
            {
                o.Expand();
            }





        }
        #region Eventos
        private void treeViewDepositos_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                grilla.Rows.Clear();
                foreach (Remito r in Sistema.getInstance().getRemitosSector((Sector)e.Node.Tag))
                {
                    grilla.Rows.Add(r.RemitoID, r.Fecha, r.Proveedor.Nombre, r.Referencia, r.Peso);
                }
            }
        }
        #endregion
    }
        
}
